package com.bossone.finddbchange;

import java.sql.*;

/**
 * JDBC工具类
 * 包含数据库连接及关闭数据库资源
 */
public class MysqlDBTools {
    static Connection connection = null;
    static PreparedStatement preparedStatement = null;
    static ResultSet resultSet = null;
    String DB_URL = "";
    String DB_USER = "";
    String DB_PWD = "";

    public MysqlDBTools(String dbUrl, String dbUser, String dbPwd) {
        DB_URL = dbUrl;
        DB_USER = dbUser;
        DB_PWD = dbPwd;
    }

    public Connection getConnection() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PWD);
        } catch (Exception e) {
            System.out.println("连接失败！");
            e.printStackTrace();
        }
        return connection;
    }


    public void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}